package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.BuyHMioAddData;
import com.alm.token.ui.data.bean.BuyHMioOrderLogData;
import com.alm.token.ui.data.bean.KuaCurrencyListData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.BuyHMioView;
import com.alm.token.ui.view.KuaCurrencyView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.SpUtil;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BuyHMioOrderListActivity extends BaseActivity implements BuyHMioView<BuyHMioOrderLogData> {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    @BindView(R.id.centerItem)
    TextView centerItem;

    private RefreshLoadMoreManager<BuyHMioOrderLogData.DataBean.ListBean> manager;
    private List<BuyHMioOrderLogData.DataBean.ListBean> dataBeans = new ArrayList<>();
    private List<BuyHMioOrderLogData.DataBean.ListBean> dataBeanListold = new ArrayList<>();
    private boolean isfull = false;

    private String limit = "100", page = "1";

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_kuacurrencylist;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.log_hint_itemdetail));
        swipeRefreshLayout.setVisibility(View.GONE);
        initManager();
    }

    private void initManager() {
        GroupAdapter adapter = new GroupAdapter(this);
        manager = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .setOpenLoadMore(true)
                .setOnLoadMoreListener(new RefreshLoadMoreManager.OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {
                        manager.getAdapter().loadMoreEnd(true);
                        manager.getAdapter().loadMoreFail();
                        preenter.getHMioOrderLogData(limit, page);
                    }
                })
                .build();
//        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//
//
//            }
//        });
        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected void loadData() {
        page = "1";
        limit = "100";
        preenter.getHMioOrderLogData(limit, page);
//        manager.setData(getTestData());
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void getData(BuyHMioOrderLogData buyHMioOrderLogData) {
        if (buyHMioOrderLogData.getStatus() == 0 && EmptyUtils.isNotEmpty(buyHMioOrderLogData.getData())) {

            if (Integer.valueOf(page) == 1) {
                dataBeans.clear();
            } else if (!isfull && dataBeans.size() > (Integer.valueOf(page) - 1) * 100) {
                dataBeans.removeAll(dataBeanListold);
            }

            if (EmptyUtils.isNotEmpty(buyHMioOrderLogData.getData().getList())) {

//            dataBeanList.clear();
                if (buyHMioOrderLogData.getData().getList().size() == 100) {
                    isfull = true;
                    page = String.valueOf(Integer.valueOf(page) + 1);
                } else {
                    isfull = false;
                }

                dataBeanListold = buyHMioOrderLogData.getData().getList();


                for (int i = 0; i < dataBeanListold.size(); i++) {
                    dataBeanListold.get(i).setLayoutId(GroupAdapter.LAYOUT_ID);
                }
                dataBeans.addAll(dataBeanListold);
                manager.setData(dataBeans);
            }

        } else {
            showToast(buyHMioOrderLogData.getMsg());
        }
        if (dataBeans.size() == 0) {
            swipeRefreshLayout.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadData();
    }

    @Override
    public void getBuyHMioAddOrder(BuyHMioAddData buyHMioAddData) {

    }


    public class GroupAdapter extends BaseAdapter<BuyHMioOrderLogData.DataBean.ListBean> {

        public static final int LAYOUT_ID = R.layout.item_buyhmio_order;

        public GroupAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, BuyHMioOrderLogData.DataBean.ListBean item, int itemType) {

            helper.setText(R.id.tv_ordersn, item.getOrderSn() + "");
            helper.setText(R.id.tv_staue, getString(item.getStatus() == 4 ? R.string.hmio_hint_oking
                    : item.getStatus() == 2 ? R.string.hmio_hint_waitpay
                    : item.getStatus() == 3 ? R.string.hmio_hint_firpay : R.string.superegg_hint_subscribe_staue_succ));
            helper.setImageDrawable(R.id.icon, getResources().getDrawable(R.drawable.icom_outdown));
            helper.setText(R.id.tv_transMoneyType, CommonUtil.saveToPoint4WithNo(item.getTransNum()) + "HMIO");
            helper.setText(R.id.tv_dollar, "$" + CommonUtil.saveToPoint2WithNo(item.getDollar())
                    +" ("+CommonUtil.saveToPoint4WithNo(item.getPayNum()) + item.getPayMoneyTypeName()+")");
            helper.setText(R.id.tv_createTime, CommonUtil.getDateTimeFromMillisecond2(item.getCreateTime()));
        }
    }
}
